package com.zmsk.zmio;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.zmsk.zmio.constant.Const;

public class MainActivity extends Activity implements View.OnClickListener {

    Button mWhiteOn, mWhiteOff, mInfraredOn, mInfraredOff, mRelayOn, mRelayOff, mRedOn, mRedOff, mGreenOn, mGreenOff;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        mWhiteOn = findViewById(R.id.white_light_on);
        mWhiteOn.setOnClickListener(this);
        mWhiteOff = findViewById(R.id.white_light_off);
        mWhiteOff.setOnClickListener(this);
        mInfraredOn = findViewById(R.id.infrared_on);
        mInfraredOn.setOnClickListener(this);
        mInfraredOff = findViewById(R.id.infrared_off);
        mInfraredOff.setOnClickListener(this);
        mRelayOn = findViewById(R.id.relay_on);
        mRelayOn.setOnClickListener(this);
        mRelayOff = findViewById(R.id.relay_off);
        mRelayOff.setOnClickListener(this);
        mRedOn = findViewById(R.id.red_light_on);
        mRedOn.setOnClickListener(this);
        mRedOff = findViewById(R.id.red_light_off);
        mRedOff.setOnClickListener(this);
        mGreenOn = findViewById(R.id.green_light_on);
        mGreenOn.setOnClickListener(this);
        mGreenOff = findViewById(R.id.green_light_off);
        mGreenOff.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.infrared_on:
                sendBroadcast(Const.ACTION_INFRARED_LED_ON);
                break;
            case R.id.infrared_off:
                sendBroadcast(Const.ACTION_INFRARED_LED_OFF);
                break;
            case R.id.relay_on:
                sendBroadcast(Const.ACTION_GATE_OPEN);
                break;
            case R.id.relay_off:
                sendBroadcast(Const.ACTION_GATE_CLOSE);
                break;
            case R.id.white_light_on:
                sendBroadcast(Const.ACTION_WHITE_LED_ON);
                break;
            case R.id.white_light_off:
                sendBroadcast(Const.ACTION_WHITE_LED_OFF);
                break;
            case R.id.red_light_on:
                sendBroadcast(Const.ACTION_RED_LED_ON);
                break;
            case R.id.red_light_off:
                sendBroadcast(Const.ACTION_RED_LED_OFF);
                break;
            case R.id.green_light_on:
                sendBroadcast(Const.ACTION_GREEN_LED_ON);
                break;
            case R.id.green_light_off:
                sendBroadcast(Const.ACTION_GREEN_LED_OFF);
                break;
        }
    }

    private void sendBroadcast(String action) {
        Intent intent = new Intent();
        intent.setAction(action);
        sendBroadcast(intent);
        Log.d("MainActivity", "send broadcast:" + action);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
